/**
 * @author 田尘殇Sean(sean.snow@live.com)
 * @date 2017/5/23
 */
export function queryUrlToObj(queryStr) {
  let tmp = queryStr.replace(/&/g, '","');
  tmp = decodeURI(tmp.replace(new RegExp('=', 'g'), '":"'));
  if (tmp === '') {
    return {};
  }
  console.log(`{"${tmp}"}`);
  return JSON.parse(`{"${tmp}"}`);
}

export default class HttpUtils {

  /**
   * 获取url中的参数
   * @returns {{} | any}
   */
  static getQueryArgs() {
    return queryUrlToObj(location.search.substr(1));
  }

  static queryToObjectWithUrl(url) {
    let tmp = url.substr(url.indexOf('?') + 1);
    return queryUrlToObj(tmp);
  }

  /**
   * 对象转换为url 字符串
   * @param {Object} params
   * @returns {String}
   */
  static urlArgs(params: Object): String {
    if (!params) {
      return '';
    }
    return Object.keys(params)
      .map(key => `${key}=${params[key] || (params[key] === 0 ? params[key] : '')}`)
      .join('&');
  }

}

